package com.byzx.wine.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.byzx.wine.po.Wineproduct;
import com.byzx.wine.service.IWineprductService;
import com.byzx.wine.util.Page;

@Controller
@RequestMapping("/wineprduct")
public class WineprductController {
	@Autowired
	private IWineprductService fs;

	@RequestMapping("fact")
	public String login(HttpServletResponse response, HttpServletRequest request) {
		return "wineprduct/wineprduct"; // winefactory.jsp路径
	}

	@RequestMapping("fff")
	@ResponseBody
	public Map<String, Object> findPage(Model model, Integer iDisplayLength, Integer sEcho, Integer iDisplayStart) {
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(iDisplayStart, iDisplayLength, map);
		Page<Map<String, Object>> page1 = fs.findPage(page);

		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}
//添加
	@RequestMapping("add")
	@ResponseBody
	public Map add(Wineproduct record) {
		int num = fs.insert(record);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("num", num);
		return map;
	}

	// 要修改先查询
	@RequestMapping("/select")
	@ResponseBody
	public Wineproduct selectByPrimaryKey(Integer id) {
		Wineproduct ww = fs.selectByPrimaryKey(id);
		return ww;
	}

	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> updateByPrimaryKeySelective(Wineproduct record) {
		int num = fs.updateByPrimaryKeySelective(record);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("num", num);
		return map;
	}
	//单个删除
   @RequestMapping("/delete")
   @ResponseBody
	public Map<String, Object> deleteByPrimaryKey(Integer id){
		int num=fs.deleteByPrimaryKey(id);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("num", num);
		return map;
    }
   //批量删除
   @RequestMapping("/deleteall")
   @ResponseBody
   public Map<String,Object> deleteall(String id){
	   String shuzu[]=id.split(",");
	   for (int i = 0; i < shuzu.length; i++) {
		int ids=Integer.parseInt(shuzu[i]);
		   int  flag=fs.deleteByPrimaryKey(ids);
	}
	   Map<String,Object> map=new HashMap<String, Object>();
	   map.put("flag",1);
	   return map;
   }
}
